﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IQKS.AWP.BusinessAccessLayer;
using IQKS.AWP.BusinessObject;

public partial class Admin_EmailByStatus : System.Web.UI.Page
{
    #region events
    Guid gudUserID = CommonUtility.GetLoggedOnUserId();
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {

            btnSave.Visible = false;
            GetActivityNameForDropdown();
            GetAllEmailSentStatusForDropdown();
            emailtemplatedropdown.Items.Insert(0, new ListItem("--Select--", "0"));
            emailtemplatedropdown.SelectedIndex = 0;
            //Session["UserID"] = Guid.NewGuid();
        }
    }

    private void GetData()
    {
        
        //if (hdnInput.Value == "search")
        //{
        //    DataSet objDs = new DataSet();
        //    objDs = GetEmailSearchData(Convert.ToInt32(emailtemplatedropdown.SelectedValue));
        //    DataView objDv = new DataView();
        //    objDv = objDs.Tables[0].DefaultView;
        //    //objDv.RowFilter = "CountryName  LIKE '*" + txtActivityTypeName.Text.Trim() + "*'";
        //    gvSearchResult.DataSource = objDv;
        //}
        //else
        //{
        //    gvSearchResult.DataSource = GetEmailSearchData(Convert.ToInt32(emailtemplatedropdown.SelectedValue));
        //}
        
        gvSearchResult.DataSource = GetEmailSearchData();
        gvSearchResult.DataBind();
    }
    
    //protected void gvSearchResult_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
    //{
    //    //throw new NotImplementedException();
    //    gvSearchResult.PageIndex = e.NewPageIndex;
    //    GetData();
    //}
 
    #endregion


    #region Methods
    private DataSet GetEmailSearchData()
    {

       DataSet objDs=null;
       EmailByStatusBO objEmailByStatusBO = new EmailByStatusBO();
            try
            {
                int SelectedActivityDropDownValue = 0;
                if (activitydropdown.SelectedValue != "0" && activitydropdown.SelectedValue != "--Select--" && activitydropdown.SelectedValue != "")
                {
                    SelectedActivityDropDownValue = Convert.ToInt32(activitydropdown.SelectedValue);
                }
                objEmailByStatusBO.ActivityID = SelectedActivityDropDownValue;

                int SelectedEmailTemplateDropDownValue = 0;
                if (emailtemplatedropdown.SelectedValue != "0" && emailtemplatedropdown.SelectedValue != "--Select--" && emailtemplatedropdown.SelectedValue != "")
                {
                    SelectedEmailTemplateDropDownValue = Convert.ToInt32(emailtemplatedropdown.SelectedValue);
                }
                objEmailByStatusBO.EmailTemplateID = SelectedEmailTemplateDropDownValue;

                int SelectedEmailStatusDropDownValue = 0;
                if (emailstatus.SelectedValue != "0" && emailstatus.SelectedValue != "--Select--" && emailstatus.SelectedValue != "")
                {
                    SelectedEmailStatusDropDownValue = Convert.ToInt32(emailstatus.SelectedValue);
                }
                objEmailByStatusBO.EmailStatusID = SelectedEmailStatusDropDownValue;
                objDs = new EmailByStatusBAL().GetEmailDataByStatus(objEmailByStatusBO);
                btnSave.Visible = false;
                if (objDs != null)
                {
                    if (objDs.Tables[0].Rows.Count > 0)
                    {
                        btnSave.Visible = true;
                    }
                }
            }

            catch (Exception exception)
            {
                exception.AddErrorToLog(); //throw new ApplicationException(exception.Message);
            }
        
        return objDs;
       
    }

    private DataSet GetActivityName()
    {
        DataSet objDs=null;
        try
        {
            objDs = new EmailByStatusBAL().GetActivityName();
        }
        catch (Exception exception)
        {
            exception.AddErrorToLog();// throw new ApplicationException(exception.Message);
        }
        return objDs;
    }

    

    private void GetActivityNameForDropdown()
    {

        DataSet objDs = new DataSet();
        objDs = new EmailByStatusBAL().GetActivityByName("EMAILTEMPLATE");
        
        activitydropdown.DataSource = objDs;
        activitydropdown.DataValueField = "ActivityID";
        activitydropdown.DataTextField = "ActivityName";
        activitydropdown.DataBind();
        activitydropdown.Items.Insert(0, new ListItem("--Select--", ""));
        activitydropdown.SelectedIndex = 0;
    }

    private void GetAllEmailSentStatusForDropdown()
    {

        DataSet objDs = new DataSet();
        objDs = new EmailByStatusBAL().GetAllEmailSentStatus();

        emailstatus.DataSource = objDs;
        emailstatus.DataValueField = "EmailSentStatusID";
        emailstatus.DataTextField = "EmailSentDisplayName";
        emailstatus.DataBind();
        emailstatus.Items.Insert(0, new ListItem("--Select--", ""));
        emailstatus.SelectedIndex = 0;
    }  
  
    private void GetEmailTemplateName(int x)
    {
        DataSet objDs = new DataSet();
        EmailByStatusBAL ebal = new EmailByStatusBAL();
       EmailByStatusBO objBO=new EmailByStatusBO();
       objBO.GetId = x;
       if (objBO.GetId != 0)
       {
           objDs = new EmailByStatusBAL().GetEmailTemplateNameByActivity(objBO.GetId);
           if (objDs != null)
           {
               emailtemplatedropdown.DataSource = objDs;
               emailtemplatedropdown.DataValueField = "EmailTemplateID";
               emailtemplatedropdown.DataTextField = "EmailSubject";
               emailtemplatedropdown.DataBind();
               emailtemplatedropdown.Items.Insert(0, new ListItem("--Select--",""));
               emailtemplatedropdown.SelectedIndex = 0;

               //if (objDs != null)
               //{
               //    if (objDs.Tables[0].Rows.Count > 0)
               //    {
               //        GetData();
               //        //objBO.GetEmailTemplateId = Convert.ToInt32(emailtemplatedropdown.SelectedValue);
               //        //GetEmailSearchData(objBO.GetEmailTemplateId);
               //    }
               //}  
                   //string m = emailtemplatedropdown.SelectedValue;
               
               //objDs=ebal.GetEmailTemplateId(m);

               //DataTable objDt = objDs.Tables[0];
               //if (objDt.Rows.Count > 0)
               //{
               //    objBO.GetEmailTemplateId = Convert.ToInt32(objDt.Rows[0]["EmailTemplateID"]);

               //    GetEmailSearchData(objBO.GetEmailTemplateId);
               //}
               //else
               //{
               //    objBO = null;
               //}
               
           }
       }
       
            //DataSet objDv = new DataSet();
        //objDv = objDs.Tables[0].DefaultView;
        //objDv.RowFilter = "CountryName  LIKE '*" + txtActivityTypeName.Text.Trim() + "*'";
        
        //gvLanguage.DataSource = objDv;
       //emailtemplatedropdown.SelectedIndex = objBO.GetId;
        //emailtemplatedropdown.DataBind();
        
    }


    



    protected void BtnClear_OnClick(object sender, EventArgs e)
    {
        //throw new NotImplementedException();
        activitydropdown.SelectedIndex = 0;
        emailtemplatedropdown.SelectedIndex = 0;
        emailstatus.SelectedIndex = 0;
        lblMessage.Text = string.Empty;
        //txtLanguageId.Text = "";
        //GetData();
    }



    protected void BtnSearch_OnClick(object sender, EventArgs e)
    {
        hdnInput.Value = "search";
        lblMessage.Text = string.Empty;
        GetData();
    }

  
   
#endregion

    protected void gvSearchResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvSearchResult.PageIndex = e.NewPageIndex;
        GetData();
    }


    
    //protected void gvLanguage_RowEditing(object sender, GridViewEditEventArgs e)
    //{
    //    gvLanguage.PageIndex = e.NewEditIndex;
    //    GetData();
    //}

    protected void activitydropdown_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (activitydropdown.SelectedValue != "--Select--")
        {
            int activityid = Convert.ToInt32(activitydropdown.SelectedValue);
            if (activityid != 0)
                GetEmailTemplateName(activityid);
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {

        EmailByStatusBAL objBal;
        EmailByStatusBO objBO;
        //Guid gudUserID = new Guid(Convert.ToString(Session["UserID"]));
        
        string strReturnValue;
        
        objBal = new EmailByStatusBAL();
        objBO = new EmailByStatusBO();

        objBO.SetMethod = "UPDATE";
        objBO.CreatedBy = gudUserID;

        string strEmailQueueID = string.Empty;
        foreach (GridViewRow row in gvSearchResult.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkBox = (CheckBox)(row.Cells[0].FindControl("chkIsActive"));                
                if (chkBox.Checked)
                {
                    int intEmailQueueID = Convert.ToInt32(row.Cells[1].Text);
                    strEmailQueueID = strEmailQueueID + "," + Convert.ToString(intEmailQueueID);
                    
                }
            }
        }

        if (strEmailQueueID.Length > 1)
        {
            objBO.EmailQueueIDs = strEmailQueueID.Substring(1, strEmailQueueID.Length - 1);
        }
        objBO.EmailSentCode = "RESEND";
        strReturnValue = (string)objBal.SetResendEmails(objBO);
        if (strReturnValue == "SUCCESS")
        {

            ClearData();
            GetData();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Record has been save succesfully.');", true);
            //lblMessage.Text = "Record has been save succesfully.";
            //lblMessage.ForeColor = System.Drawing.Color.Green;

        }
        else if (strReturnValue == "ERROR")
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Error while saving record.');", true);
            //lblMessage.Text = "Error while saving record."; //EnrollResources.GetMessage(Message.FailedMessage); // 
            //lblMessage.ForeColor = System.Drawing.Color.Red;
        }


    }

    private void ClearData()
    {
        lblMessage.Text = string.Empty;
    }

}